package config

import (
	"go.uber.org/zap/zapcore"
)

// Conf 全局配置变量
var Conf = new(config)

type config struct {
	System   *SystemConfig
	Logs     *LogsConfig
	Postgres *PostgresConfig
	Mysql    *MysqlConfig
	Redis    *RedisConfig
}

type SystemConfig struct {
	Mode          string `env:"MODE" default:"debug"`            // 设置 gin 模式
	UrlPathPrefix string `env:"URL_PATH_PREFIX"`                 // url前缀
	Port          int    `env:"PORT" default:"777"`              // 服务端口
	InitData      bool   `env:"INIT_DATA"`                       // 不知道是啥
	FilePath      string `env:"FILE_PATH" default:"./pictures/"` // 文件上传路径
	Driver        string `env:"DRIVER" default:"Postgres"`       // 数据库
	Domain        string `env:"DOMAIN" default:"localhost"`      // cookie域
}

type LogsConfig struct {
	Level      zapcore.Level `env:"LOGS_LEVEL" default:"-1"`
	Path       string        `env:"LOGS_PATH" default:"./log"`
	MaxSize    int           `env:"MAX_SIZE" default:"1024"`
	MaxBackups int           `env:"MAX_BACKUPS" default:"10"`
	MaxAge     int           `env:"MAX_AGE" default:"7"`
	Compress   bool          `env:"COMPRESS"  default:"true"`
}

type PostgresConfig struct {
	Username string `env:"POSTGRES_USERNAME" default:"postgres"`
	Password string `env:"POSTGRES_PASSWORD" default:"postgres"`
	Database string `env:"POSTGRES_DATABASE" default:"first-go-service"`
	Host     string `env:"POSTGRES_HOST" default:"localhost"`
	Port     int    `env:"POSTGRES_PORT" default:"5432"`
	LogMode  bool   `env:"POSTGRES_LOG_MODE" default:"false"`
}

type MysqlConfig struct {
	Username string `env:"MYSQL_USERNAME" default:"root"`
	Password string `env:"MYSQL_PASSWORD" default:"root"`
	Database string `env:"MYSQL_DATABASE" default:"gps-manage"`
	Host     string `env:"MYSQL_HOST" default:"127.0.0.1"`
	Port     int    `env:"MYSQL_PORT" default:"3306"`
	LogMode  bool   `env:"MYSQL_LOG_MODE"`
}

type RedisConfig struct {
	Addr         string `env:"REDIS_ADDR" default:"localhost:6379"`
	Password     string `env:"REDIS_PASSWORD" default:""`
	DB           int    `env:"REDIS_DB" default:"0"`
	PoolSize     int    `env:"REDIS_POOL_SIZE" default:"10"`
	MinIdleConns int    `env:"REDIS_MINIDLE_CONNS" default:"2"`
}

// InitConfig 设置读取配置信息
func InitConfig() *config {
	var c config
	c.getSystemConfig()
	c.getLogsConfig()
	// c.getPostgresConfig()
	// c.getMysqlConfig()
	// c.getRedisConfig()
	// c.getGpsServiceConfig()
	// c.getTrashcanServiceConfig()
	return &c

}

func (c *config) getSystemConfig() {
	if c.System != nil {
		return
	}
	systemConfig := new(SystemConfig)
	if err := env.Fill(systemConfig); err != nil {
		panic(err)
	}
	c.System = systemConfig
}

func (c *config) getLogsConfig() {
	if c.Logs != nil {
		return
	}
	logsConfig := new(LogsConfig)
	if err := env.Fill(logsConfig); err != nil {
		panic(err)
	}
	c.Logs = logsConfig
}

func (c *config) getPostgresConfig() {
	if c.Postgres != nil {
		return
	}
	postgresConfig := new(PostgresConfig)
	if err := env.Fill(postgresConfig); err != nil {
		panic(err)
	}
	c.Postgres = postgresConfig
}

func (c *config) getMysqlConfig() {
	if c.Mysql != nil {
		return
	}
	mysqlConfig := new(MysqlConfig)
	if err := env.Fill(mysqlConfig); err != nil {
		panic(err)
	}
	c.Mysql = mysqlConfig
}

func (c *config) getRedisConfig() {
	if c.Redis != nil {
		return
	}
	redisConfig := new(RedisConfig)
	if err := env.Fill(redisConfig); err != nil {
		panic(err)
	}
	c.Redis = redisConfig
}
